import os
from openai import OpenAI

try:
	client = OpenAI(
		# 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
		# api_key=os.getenv("DASHSCOPE_API_KEY"),
		api_key="sk-a13021e4fc804e1097d4a80f3cd7258c",
		base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
	)

	completion = client.chat.completions.create(
		# 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
		model="qwen-plus",
		messages=[
			{"role": "system", "content": "You are a helpful assistant."},
			# {"role": "user", "content": "java String类常用的方法有那些"},
			{"role": "user", "content": "你是谁？"},
		],
	)
	print(completion.choices[0].message.content)
except Exception as e:
	print(f"错误信息：{e}")
	print("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
